package com.dp.appi.controller;

/**
 * Created by bchen on 2016/5/21.
 */

import com.dp.appi.entity.UserEntity;
import com.dp.appi.exception.CommonException;
import com.dp.appi.service.ConsumerService;
import com.dp.appi.service.OrgUserService;
import com.dp.appi.service.ProjectService;
import com.dp.appi.util.ExceptionUtil;
import com.dp.appi.util.SYS;
import com.dp.appi.util.StringUtil;
import org.apache.commons.lang.ObjectUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;

/**
 * Created by bchen on 2016/5/19.
 */
@Controller
@RequestMapping(value = "/orgUserControl")
public class OrgUserControl {

    private static Logger log = LoggerFactory.getLogger(OrgUserControl.class);
    @Autowired
    private OrgUserService orgUserService;

    @Autowired
    private ConsumerService consumerService;

    @Autowired
    private ProjectService projectService;

    /**
     * 获取单位列表
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/orgList",method = RequestMethod.GET)
    public void orgList(ModelMap modelMap, @RequestParam Map<String,String> param) {
        try {

            String openIdent = param.get("openIdent");
            UserEntity userEntity = UserEntity.decrypt(openIdent);
            String username = userEntity.getA();

            //获取当前登陆人员的单位信息
            Map<String,Object> org = orgUserService.getOrgByUsername(username);
            if(org==null){
                throw new CommonException("EXPO011");
            }

            String[] orgArr =param.get("orgType").split("\\|");
            for(int i=0;orgArr!=null&&i<orgArr.length;i++){
                if(orgArr[i].equals(String.valueOf(org.get("org_type")))||"297e640454cbba690154cd8eb0460010".equals(String.valueOf(org.get("id")))){
                   // 返回登陆人的组织单位-不包含子级
                    List<Map<String,Object>> orgList = new ArrayList<>();
                    orgList.add(org);
                    modelMap.addAttribute(orgArr[i],orgList);

                    //返回登陆人的组织单位-包含子级
//                    modelMap.addAttribute(orgArr[i],orgUserService.orgChildList(String.valueOf(org.get("id")),String.valueOf(org.get("org_type"))));
                }else {
                    modelMap.addAttribute(orgArr[i],orgUserService.orgList(orgArr[i]));
                }
            }

        }catch (CommonException c) {
            modelMap.addAllAttributes(SYS.valueOf(c.getMessage()).msgMap());
        }catch (Exception e){
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 获取人员列表
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/userByTypeList",method = RequestMethod.GET)
    public void userByTypeList(ModelMap modelMap, @RequestParam Map<String,String> param) {
        try {



            String openIdent = param.get("openIdent");
            UserEntity userEntity = UserEntity.decrypt(openIdent);
            String username = userEntity.getA();
            Map<String, Object> map = consumerService.getUser(userEntity);
            String userId = String.valueOf(map.get("id"));
            String userstage_idId = "";
            String stage_id = param.get("stage_id");
            //获取阶段创建人
            if(!StringUtil.isEmpty(stage_id)){
                Map<String,Object> psInfo = projectService.projectStageInfo(Long.parseLong(stage_id));
                if(null!=psInfo&&StringUtil.isNotEmpty(psInfo.get("creater"))){
                    userId = String.valueOf(psInfo.get("creater"));
                    userstage_idId = String.valueOf(psInfo.get("explorationer"));
                }

            }


            //获取当前登陆人员的单位信息
            Map<String,Object> org = orgUserService.getOrgByUsername(username);
            if(org==null){
                throw new CommonException("EXPO011");
            }


            String design_unit = String.valueOf(org.get("id"));
//            String design_unit = param.get("design_unit");
//            String owner_unit = param.get("owner_unit");
            String userTypeStr = param.get("userType");
            String[] userTypeArr = userTypeStr.split("\\|");
            for(int i=0;userTypeArr!=null&&i<userTypeArr.length;i++){

                if("297e640454cbba690154cd8eb0460010".equals(String.valueOf(org.get("id")))){
                    modelMap.addAttribute(userTypeArr[i],orgUserService.userByUsernameList(username,userstage_idId));

                }else {
                    modelMap.addAttribute(userTypeArr[i],orgUserService.userByTypeList(design_unit,userTypeArr[i],userId));
                }

            }
        }catch (CommonException c) {
            modelMap.addAllAttributes(SYS.valueOf(c.getMessage()).msgMap());
        }catch (Exception e){
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }


    @RequestMapping(value = "/userByPhoneList",method = RequestMethod.GET)
    public void userByPhoneList(ModelMap modelMap, @RequestParam Map<String,String> param) {
        try {


            String userphone = param.get("userphone");
            modelMap.addAttribute("12",orgUserService.userByUserPhoneList(userphone));

        }catch (CommonException c) {
            modelMap.addAllAttributes(SYS.valueOf(c.getMessage()).msgMap());
        }catch (Exception e){
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

}
